package com.itic.ht.cms.exam.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.cms.exam.persistence.mapper.ExamMapper;
import com.itic.ht.cms.exam.persistence.model.Exam;
import com.itic.ht.cms.exam.persistence.model.ExamExample;
import com.itic.ht.cms.question.persistence.model.QuestionModel;
import com.itic.ht.mem.mtype.persistence.model.WebmType;

@Service
@Transactional
public class ExamService {
	
	protected static final String SUCCESS = ResponseCode.SUCCESS.getCode();
    protected static final String FAILED = ResponseCode.SERVICE_ERROR.getCode();
	
	@Autowired
	private ExamMapper mapper;
	
	public int count(ExamExample example) {
		return mapper.countByExample(example);
	}

	public List<Exam> list(ExamExample example) {
		return mapper.selectByExample(example);
	}

	public Exam get(String id) {
		if (StringHelper.isNotEmpty(id)) {
            return mapper.selectByPrimaryKey(id);
        }
        return null;
	}

	public String add(Exam templet) {
		int code = mapper.insert(templet);
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
	}

	public String edit(Exam temp) {
		int code = mapper.updateByPrimaryKeySelective(temp);
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
	}

	public String delete(Exam model) {
		int code = 0;
        String id = model.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
            if (null == model.getMtime()) {
                model.setMtime(new Date());
            }
            model.setDelFlag(Integer.parseInt(DBConst.FALSE));
            code = mapper.updateByPrimaryKeySelective(model);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }	}

	public Exam getExamName(String examId) {
		return mapper.selectByPrimaryKey(examId);
	}
}
